﻿/*******************************************************************\
| Author(s):    Gary Kelly gkelly2008@my.fit.edu                    |
| Date:         Oct 27, 2011                                        |
| Class:        SaveAWatt                                           |
| Descr:        My class does this stuff                            |
\*******************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.Threading;

//using SaveAWatt.Console;
using SaveAWatt.GUI;
using SaveAWatt.Tests;
using SaveAWatt.EMMObjects;
using SaveAWatt.Server;

using log4net;
using log4net.Config;

namespace SaveAWatt
{
    // Starting class for the SaveAWatt system
    public class SaveAWatt
    {
        // Logger
        //
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static SAWGUI guiForm;
        private static GUITest scmReference;
        private static bool guiOpen;
        private Action updateDelegate = new Action(UpdateListMethod);

        //CmdPrompt.Instance = new CmdPrompt();

        public static void Main(string[] args)
        {
            /*System.IO.FileInfo file = new System.IO.FileInfo("..\\..\\..\\LogFormat.config");
            if (!file.Exists)
            {
                System.Console.WriteLine("Config file not found");
                System.Console.Read();
                return;
            }
            XmlConfigurator.Configure(file);*/

            System.Console.WriteLine("[SaveAWatt] Starting SaveAWatt system...\n");

            // See if we are set to run in the background
            // --Change this to utilize a config file
            bool background = false;

            if (background)
            {
                //class for running in background
            }
            else
            {
                //run .Startup() code

                SaveAWatt currentInstance = new SaveAWatt();

                scmReference = new GUITest();
                guiOpen = false;

                Thread thGui = new Thread(currentInstance.showGuiForm);
                guiOpen = true;
                thGui.Start();

                WebServer httpServer = new WebServer();
                httpServer.Initialise ();
                Thread thServer = new Thread(httpServer.Listen);
                thServer.Start();

                while (!SAWGUI.isLoaded())
                {
                    Thread.Yield();
                }
                guiForm.BeginInvoke(currentInstance.updateDelegate);

                while (guiOpen)
                {
                    try
                    {
                        // Actions
                        Thread.Yield();
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("[SaveAWatt] Command error: {0}", e);
                    }
                }
            }
        }//end main

        private void showGuiForm()
        {
            // All ye threads who come here shall be blocked lest the GUI fall.
            guiForm = new SAWGUI();
            Application.Run(guiForm);
            guiOpen = false;
        }

        private static void UpdateListMethod()
        {
            guiForm.setList(scmReference.getList());
            guiForm.intializeEmmList();
        }
    }
}
